//
// Created by Tony on 2024/5/19.
//

#ifndef LEXER_LEXER_H
#define LEXER_LEXER_H

#include "../common/ErrorRecorder.h"
#include "../file_reader/SourceFile.h"
#include "TokenQueue.h"
#include "token/Token.h"

namespace Lexer {

class Lexer : public ErrorRecorder {
    static constexpr uint16_t MAX_NAME_LEN = 128;
    uint16_t lineNo = 0;
    uint16_t cursor = 0;
    string text;

public:
    /**
     * @return false: parse failed
     */
    bool parseString(const string& text, uint16_t lineNo, TokenQueue& tokenQueue);

    /**
     * @return false: parse failed
     */
    bool parseFile(const shared_ptr<SourceFile>& file, TokenQueue& tokenQueue);

private:
    bool parseToken(shared_ptr<Token>& token);

    char getChar();
    void unGetChar();

    void skipSpace();
    void skipComment();

    bool parseIdentifier(string& identifier);
    bool parseNumber(shared_ptr<Token>& token);
    bool parseStringLiteral(shared_ptr<Token>& token);
    bool parseCharLiteral(shared_ptr<Token>& token);
    bool parsePunct(shared_ptr<Token>& token);
};

} // namespace Lexer

#endif // LEXER_LEXER_H
